﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Psl.Chat;

namespace Stl.Chat.Components
{
    public partial class ChatControler : UserControl
    {
        /************************************************************
         *                          Champs                          *
         ************************************************************/
        protected IChat model;

        protected string userID;

        /************************************************************
         *                        Constructeur                      *
         ************************************************************/
        public ChatControler()
        {
            InitializeComponent();
            this.Enabled = false;
            this.model = ChatRegistry.ChatState.ModelInternal;
            if(model != null)
                this.userID = model.GetNewUserID();
        }

        /************************************************************
         *                         Evenements                       *
         ************************************************************/
        private void sendButton_Click(object sender, EventArgs e)
        {
            comboBoxIndex.TextEnter(comboBoxIndex.Text);
            DoSendMessage(comboBoxIndex.Text);
            DoSelectText();
            comboBoxIndex.Focus();
        }

        private void comboBoxIndex_Validated(object sender, EventArgs e)
        {
            DoSendMessage(comboBoxIndex.Text);
            DoSelectText();
        }

        private void chatEvents_ChatOpened(object sender, EventArgs e)
        {
            if (model == null)
            {
                model = ChatRegistry.ChatState.ModelInternal;
                userID = model.GetNewUserID();
                userLabel.Text = userID;
            }
            this.Enabled = true;
        }

        private void chatEvents_ChatClosing(object sender, EventArgs e)
        {
            userLabel.Text = "Pseudo";
            this.Enabled = false;
        }

        /************************************************************
         *                          Méthodes                        *
         ************************************************************/
        protected void DoSendMessage(String msg)
        {
            model.Add(userID, msg);
        }

        // Selectionner le texte contenu dans le combobox
        private void DoSelectText()
        {
            comboBoxIndex.SelectAll();
        }

    }
}
